<?php

namespace app\reception\validate;

use think\Validate;

class UserValidate extends Validate
{
    protected $rule = [
        'userId' => 'require',//设备唯一标识。在 Android 上返回 androidid
        'provider' => 'require|max:20',
        'regId' => 'require',//PushService 返回的注册 id，可用于针对某个用户发送消息
        'phone' => 'require|mobile',
        'code' => 'require|length:4',
        'content' => 'require|max:255',
        'from_id' => 'require|number|gt:0',
        'nids' => 'require|checkIds',
        'month' => 'require',
        'page' => 'number|egt:1',
        'limit' => 'number|egt:1|elt:30',
    ];

    protected $message = [

    ];

    protected $scene = [
        'auth' => ['userId'],
        'subscribe' => ['regId'],
        'setProvider' => ['provider'],
        'smsCode' => ['phone'],
        'binding' => ['phone', 'code'],
        'addFeedBack' => ['content'],
        'addComments' => ['from_id', 'content'],
        'delReadRecord' => ['nids'],
        'consumeRecord' => ['month', 'page', 'limit']
    ];

    // 自定义验证规则
    protected function checkIds($value, $rule, $data = [])
    {
        if (!preg_match('#^[1-9][0-9]*(,[1-9][0-9]*)*$#', $value)) {
            return '参数异常';
        }
        return true;
    }
}